import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/page_status_com.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';

import '../../module_trade/widgets/warn_tips.dart';
import 'progressWidget.dart';

class BeginnerTasksPageList extends StatefulWidget {
  final List<IntegralTabsItemDataType> list;
  final Function onClick;
  final int pageStatus;
  final String title;

  // final List<int> list;
  final bool hasMore;

  const BeginnerTasksPageList(
      {super.key,
      required this.list,
      required this.pageStatus,
      required this.title,
      this.hasMore = false,
      required this.onClick});

  @override
  State<BeginnerTasksPageList> createState() => _BeginnerTasksPageListState();
}

class _BeginnerTasksPageListState extends State<BeginnerTasksPageList> {
  List<IntegralTabsItemDataType> get list => widget.list;

  bool get hasMore => widget.hasMore;

  int get pageStatus => widget.pageStatus;
  String get title => widget.title;

  bool btnStatus = false;

  String btnText(IntegralTabsItemDataType item) {
    int status= item.status;
    if (status == 2) {
      return 'Claim';
    }
    if (status == 1 || status == 0 ) {
      return [206,309,310,311].contains(item.task_code)? 'Share':'Complete';
    }
    return 'Completed';
  }

  ButtonType btnType(int status) {
    if (status == 1) {
      return ButtonType.deleteComplete;
    }
    return ButtonType.private;
  }

  Widget BeginnerTasksItem(IntegralTabsItemDataType item, int index) {
    return Container(
      margin: EdgeInsets.only(top: index != 0 ? 0.w : 15.w, bottom: 10.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.w),
        border: Border.all(
          color: Color.fromRGBO(224, 224, 224, 1),
          width: 1.w,
        ),
      ),
      padding: EdgeInsets.symmetric(vertical: 15.w, horizontal: 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          PublicImage(
            image: item.icon,
            width: 40.w,
            height: 40.w,
          ),
          // if (item.status == 3)
          //   PublicImage(
          //     image: 'assets/images/integral/undone.png',
          //     width: 40.w,
          //     height: 40.w,
          //   ),
          // if (item.status == 1)
          //   PublicImage(
          //     image: 'assets/images/integral/done.png',
          //     width: 46.w,
          //     height: 35.w,
          //   ),
          Container(
              width: 233.w,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item.title,
                    style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w500,
                        color: Color.fromRGBO(51, 51, 51, 1)),
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  (item.task_code == 303 || item.task_code == 308)
                      ? progressWidget(rules:item.describe)
                      : RichText(
                          softWrap: true,
                          maxLines: 2,
                          overflow: TextOverflow.visible,
                          text: TextSpan(
                            text: item.describe +', ',
                            style: TextStyle(
                                color: Color.fromRGBO(134, 131, 145, 1),
                                fontSize: 12.sp,
                                fontWeight: FontWeight.w400),
                            children: [
                              TextSpan(
                                  text: '+${item.score.toString()} KC',
                                  style: TextStyle(
                                      color: Color.fromRGBO(134, 103, 254, 1),
                                      fontSize: 12.sp,
                                      fontWeight: FontWeight.w400)),
                            ],
                          ),
                        ),
                  SizedBox(
                    height: 10.w,
                  ),
                  Visibility(
                    visible: !((item.status == 0 || item.status == 1) &&
                        (item.task_code == 312 ||
                            item.task_code == 313)), //特殊处理，如果是签到任务，那就不用显示去完成的按钮
                    child: Container(
                      width: 80.w,
                      height: 30.w,
                      child: PublicButton(
                        text: btnText(item),
                        margin: EdgeInsets.zero,
                        type: btnType(item.status),
                        disabledStatus: item.status == 0 || item.status == 3,
                        onClick: () async {
                          if (btnStatus) {
                            return;
                          }
                          btnStatus = true;
                          await widget.onClick(item);
                          btnStatus = false;
                        },
                      ),
                    ),
                  )
                ],
              ))
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // super.build(context);
    return Container(
      margin: EdgeInsets.only(left: 20.w, right: 20.w),
      child: ListView.builder(
          padding: EdgeInsets.only(top: 0, bottom: 0),
          physics: const ClampingScrollPhysics(),
          itemCount: list.length + 1,
          itemBuilder: (context, index) {
            if (list.isEmpty) {
              return Container(
                padding: EdgeInsets.only(top: 0.w),
                color: Colors.transparent,
                child: PageEmptyCom(title: 'Empty data'),
              );
            }
            if (index == list.length) {
              return Container(
                padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                alignment: Alignment.center,
                child: Text(
                  'Already at the bottom',
                  style: TextStyle(
                      fontSize: 12.sp,
                      color: Color.fromRGBO(190, 189, 202, 1),
                      fontWeight: FontWeight.w400),
                ),
              );
            }
            var item = list[index];
            if(index==0){
              if(pageStatus == 0){
                return Column(
                  children: [
                    Container(child: WarnTips(text: "Complete beginner tasks to unlock ${title.toLowerCase()}!"),margin: EdgeInsets.only(bottom: 0.w),),
                    BeginnerTasksItem(item, index)
                  ],
                );
              }
            }
            return BeginnerTasksItem(item, index);
          }),
    );
  }
}
